package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.query.NewsQueryObject;
import cn.wolfcode.trip.base.service.INewsService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("news")
public class NewController {

    @Autowired
    private INewsService newsService;

    /**
     * 所有日报
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String user(@ModelAttribute("qo") NewsQueryObject qo, Model model){
        //按照推荐时间倒序排列
        qo.setOrderBy("n.commendTime desc");
        model.addAttribute("pageInfo",newsService.listAll(qo));
        return "news/list";
    }
    /**
     * 根据id获取日报详情内容
     * @param id
     * @return
     */
    @RequestMapping("getContentById")
    @ResponseBody
    public NewsContent getById(Long id){

        return newsService.getContentByNewsId(id);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(MultipartFile file, News entity) {
        //有文件就上传
        if (file!=null&&file.getSize()>0){
            String url = UploadUtil.upload(file, UploadUtil.PATH + "/upload");
            entity.setCoverUrl(url);
        }
        newsService.saveOrUpdate(entity);
        return new JsonResult();
    }
}
